package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ClockInLogic;
import cn.topevery.garden.entity.dto.ClockInDTO;
import cn.topevery.garden.entity.fetch.ClockFetch;
import cn.topevery.garden.entity.po.ClockIn;
import cn.topevery.garden.entity.query.ClockInQuery;
import cn.topevery.garden.entity.vo.ClockInVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.entity.po.SysDept;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import static cn.topevery.garden.framework.util.CurrentUserUtil.getDept;


/**
 * @author code-generate
 * @date 2022-01-07
 */
@Api(tags = "手机打卡")
@RestController
@RequestMapping("/clock")
public class ClockInController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    ClockInLogic clockInLogic;

    @Autowired
    private ClockInLogic clockInService;


    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<ClockInVO>> listPage(@RequestBody ClockInQuery query) {
        return JsonResult.ok(clockInLogic.listPage(query));
    }

    @ApiOperation("手机打卡接口")
    @PostMapping("/clockIn")
    public JsonResult<?> clockIn(@Valid @RequestBody ClockInDTO dto) {
        return clockInLogic.clockIn(getUser(), getDept(), dto);
    }

    @ApiOperation("查看个人当天打卡信息")
    @GetMapping("/clockInfoToday")
    public JsonResult<List<ClockIn>> clockInfoToday() {
        return JsonResult.ok(clockInLogic.clockInfoToday(getUser().userId));
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<ClockInVO> getInfoById(@RequestParam Integer id) {
        ClockIn entity = clockInLogic.getEntity(id);
        ClockInVO vo = mapper.map(entity, ClockInVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        clockInLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(ClockInQuery query, HttpServletResponse response) {
        List<ClockInVO> list = clockInLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", ClockInVO.class, "", response);
    }


    @ApiOperation(value = "列表分页查询")
    @PostMapping(value = "/getList")
    public JsonResult<?> getList(@RequestBody ClockFetch para) {
        DataPager<ClockIn> pageRes = clockInService.getList(para);
        return JsonResult.ok(pageRes);
    }

    @ApiOperation(value = "打卡")
    @PostMapping(value = "/insert")
    public JsonResult<?> insert(@RequestBody ClockIn clock) {
        SysUser user = getUser();
        SysDept dept = getDept();
        LocalDateTime now = LocalDateTime.now();
        clock.setEntityId(UUID.randomUUID().toString());
        clock.setUserId(user.userId);
        clock.setName(user.userName);
        clock.setCompanyName(dept.deptName);
        clock.setDate(now);
        clock.setCreateTime(now);
        clock.setCreateId(user.id.toString());
        clock.setDbStatus(true);
        clock.setIsRange(1);
        clock.setIsUse(1);
        clock.setIsValid(1);

        ClockInQuery fetch = new ClockInQuery();
        fetch.setBeginDate(LocalDateTime.now().toString());
        fetch.setType(clock.getType());
        fetch.setIsUse(1);
        DataPager<ClockInVO> list = clockInLogic.listPage(fetch);
        for (ClockInVO in : list.records) {
            in.setIsUse(0);
            clockInService.saveOrUpdate(user.id.toString(), mapper.map(in, ClockIn.class));
        }
        clockInService.saveOrUpdate(user.id.toString(), clock);
        return JsonResult.ok(true);
    }

    @ApiOperation(value = "个人打卡记录")
    @GetMapping(value = "/getPerson")
    public JsonResult<?> getPerson() {
        SysUser user = getUser();
        LocalDateTime now = LocalDateTime.now();
        String today = now.toString().substring(0, 10);
        Integer to = Integer.parseInt(now.toString().substring(8, 10)) + 1;
        String tomorrow = now.toString().substring(0, 8).concat(to.toString());
        ClockInQuery query = new ClockInQuery();
        query.setBeginDate(today);
        query.setEndDate(tomorrow);
        query.setUserId(user.id.toString());
        List<ClockIn> clockInList = clockInService.getClockInList(query);
        return JsonResult.ok(clockInList);
    }

    @ApiOperation("判断当前人员是否在标段范围内")
    @GetMapping("/isInSectionRange")
    public JsonResult<?> isInSectionRange(@RequestParam Double lng, @RequestParam Double lat,
                                          @RequestParam(required = false, defaultValue = "300D") Double offset) {
        return JsonResult.ok(clockInLogic.userIsInSectionRange(getUser(), lng, lat, offset));
    }

}
